<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>提交一个任务到特定的 Worker 以执行</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pool.submit.html">« Pool::submit</a></li>
      <li style="float: right;"><a href="class.volatile.html">Volatile »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.pool.html">Pool</a></li>
    <li>提交一个任务到特定的 Worker 以执行</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pool.submitTo" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Pool::submitTo</h1>
  <p class="verinfo">(PECL pthreads &gt;= 2.0.0)</p><p class="refpurpose"><span class="refname">Pool::submitTo</span> &mdash; <span class="dc-title">提交一个任务到特定的 Worker 以执行</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pool.submitTo-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>Pool::submitTo</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$worker</code></span>, <span class="methodparam"><span class="type"><a href="class.threaded.html" class="type Threaded">Threaded</a></span> <code class="parameter">$task</code></span>): <span class="type">int</span></div>

  <p class="para rdfs-comment">
   将对象提交到 Pool 中某个特定的 Worker 对象来执行。Worker 的下标从 0 开始，
   由于 Pool 中的线程是懒加载机制，
   所以 Worker 对象仅在 Pool 需要执行任务的时候才会真正被创建。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pool.submitTo-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">worker</code></dt>

    <dd>

     <p class="para">
      用来执行任务的 Worker 对象，下标从 <code class="literal">0</code> 开始。
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">task</code></dt>

    <dd>

     <p class="para">
  要执行的任务
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-pool.submitTo-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
 接受新加入对象的 Worker 对象ID
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pool.submitTo-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3346">
    <p><strong>示例 #1 提交任务到特定的 Worker</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Task&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Threaded&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">run</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">Thread</span><span style="color: #007700">::</span><span style="color: #0000BB">getCurrentThreadID</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$pool&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Pool</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$pool</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">submit</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">Task</span><span style="color: #007700">());<br /><br />for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;&nbsp;++</span><span style="color: #0000BB">$i</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$pool</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">submitTo</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">Task</span><span style="color: #007700">());&nbsp;</span><span style="color: #FF8000">//&nbsp;将所有的任务都入栈到下标为&nbsp;0&nbsp;的&nbsp;Worker<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$pool</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">submitTo</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">Task</span><span style="color: #007700">());&nbsp;</span><span style="color: #FF8000">//&nbsp;由于第二个&nbsp;Worker&nbsp;尚未存在，所以不可以将任务入栈到第二个&nbsp;Worker<br /><br /></span><span style="color: #0000BB">$pool</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">shutdown</span><span style="color: #007700">();</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(4475011072)
int(4475011072)
int(4475011072)
int(4475011072)
int(4475011072)
int(4475011072)

Fatal error: Uncaught Exception: The selected worker (1) does not exist in %s:%d
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>